package top.jiaman.javaweb.servlet.user;

import com.mysql.cj.util.StringUtils;
import top.jiaman.javaweb.dao.UserDAOImpl;
import top.jiaman.javaweb.entity.BaseResponse;
import top.jiaman.javaweb.entity.User;
import com.google.gson.Gson;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;


/**
 * @Author Jiaman
 * @Date 2020/11/20 10:50
 * @Desc
 */

@WebServlet(urlPatterns = "/user/all")
public class UserAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");


        UserDAOImpl us = UserDAOImpl.getInstance();
        List<User> users = us.getUser();


        BaseResponse<List<User>> userBaseResponse = new BaseResponse<List<User>>();
        userBaseResponse.setCode(200);
        userBaseResponse.setMsg("请求成功");
        userBaseResponse.setData(users);

        Gson gson = new Gson();
        String json = gson.toJson(userBaseResponse);
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.print(json);
        out.flush();
    }
}
